package com.zy.hospitalward_management_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zy.hospitalward_management_system.pojo.Staff;
import com.zy.hospitalward_management_system.mapper.StaffMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zy
 * @since 2021-01-26
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IService<Staff> {


    @Resource
    StaffMapper staffMapper;

    //用户输入账号密码登录
    //返回 -1 账号不存在
    //返回 1 登陆成功
    //返回 0 密码错误
    //500 系统错误
    public int login(String username, String password, HttpSession session){
        int flag = 500;

        //根据账号从数据库读取数据 封装到Account对象里
        Staff staff = staffMapper.selectOne(new QueryWrapper<Staff>().eq("staff_username", username));

        //如果账号不存在
        if(staff==null){
            flag = -1;
            return flag;
        }

        //如果密码错误
        if (!staff.getStaffPassword().equals(password)){
            flag = 0;
        }

        //登陆成功
        if(staff.getStaffPassword().equals(password)){
            //如果登陆成功 往Session域里面放置loginUser标识
            session.setAttribute("loginUser", staff);
            flag = 1;
        }


        return flag;
    }
}
